package routing;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Producer {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("162.14.99.141");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("admin");
        connectionFactory.setPassword("admin");
        connectionFactory.setVirtualHost("ty");
        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();

        //声明交换机
        channel.exchangeDeclare("routing.exchange", BuiltinExchangeType.DIRECT, true, false, false, null);

        channel.queueDeclare("routing.queue1", true, false, false, null);
        channel.queueDeclare("routing.queue2", true, false, false, null);

        //交换机和队列进行绑定
        channel.queueBind("routing.queue1", "routing.exchange", "routing1");
        channel.queueBind("routing.queue2", "routing.exchange", "routing2");

        for (int i = 0; i < 50; i++) {
            //广播到指定routingkey的队列上
            channel.basicPublish("routing.exchange", "routing" + (i % 2 + 1), null, ("msg" + i).getBytes());
        }

        channel.close();
        connection.close();
    }
}
